package com.zl.service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.system.common.mapper.BisCustomUserMapper;
import com.system.common.pojo.BisCustomUser;
import com.system.common.pojo.BisCustomUserExample;
import com.system.common.pojo.BisCustomUserExample.Criteria;
import com.wechat.vo.WechatUser;

@Service
public class WechatCommonService {

	@Autowired
	private BisCustomUserMapper userMapper;
	public BisCustomUser saveCustomInfo(WechatUser user,String rootPath){
		BisCustomUserExample example=new BisCustomUserExample();
		Criteria cri=example.createCriteria();
		cri.andOpenidEqualTo(user.getOpenid());
		cri.andIsDelEqualTo("0");
		List<BisCustomUser> list=userMapper.selectByExample(example);
		BisCustomUser customUser=null;
		int flag=0;
		if(list.size()>0){
			customUser=list.get(0);
			flag=1;
		}else{
			customUser=new BisCustomUser();
			customUser.setCustomUserId(UUID.randomUUID().toString());
			flag=0;
		}
		/*String url=user.getHeadimgurl();
		if(Tools.isNotNull(url)){
			String fileName=customUser.getCustom_user_id()+".png";
			String folderPath = rootPath+File.separator+"/imageFile/customImage/";
			FileUtil.createFolder(folderPath);
			String saveFilePath=folderPath+File.separator+fileName;
			FileUtil.downloadUrlFile(url,saveFilePath);//涓嬭浇澶村儚淇℃伅
			customUser.setPhoto("/imageFile/customImage/"+fileName); //瀛樺偍鐢ㄦ埛鐓х墖URL鍦板潃
		}*/
		
		System.out.println(user.getHeadimgurl()+"<><><>");
		customUser.setOpenid(user.getOpenid());
		customUser.setNikename(user.getNickname());
		customUser.setSex(user.getSex()+"");
		customUser.setRemark(user.getRemark());
		customUser.setAddress(user.getProvince()+user.getCity());
		customUser.setEnrollMode("1");
		customUser.setEnrollTime(new Date());
		customUser.setWechatStatus("2");
		if(flag==0){

			userMapper.insertSelective(customUser);
		}else{

			userMapper.updateByPrimaryKeySelective(customUser);
		}	
		return customUser;
		}
	
	public void updateCustomUser(BisCustomUser user){
		this.userMapper.updateByPrimaryKeySelective(user);
	}
}
